﻿/* Copyright (C) Siemens AG, 2012. All Rights Reserved.
 * Author: Roland Eckl, Corporate Technology
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using RVM.Helpers;
using RVM.Infrastructure;
using RVM.Infrastructure.Communication;
using RVM.Infrastructure.Communication.WCF.NetPipe;
using RVM.Infrastructure.Identification.Process;
using RvmShared;


namespace RvmExporterConsole
{
    class Program
    {
        #region Private Members

        /// <summary>
        /// Registry (for testing purpose)
        /// </summary>
        private static ViewModelRegistry _registry = new ViewModelRegistry();

        /// <summary>
        /// Sample ViewModel which is exported.
        /// </summary>
        private static ViewModelParent _viewModelToShare = new ViewModelParent();

        /// <summary>
        /// Exporter, exports sample ViewModel.
        /// </summary>
        private static IViewModelExporter _exporter = new NetPipeViewModelExporter(
            new ProcessIdentificationProvider(SimpleSession.Current, SimpleSession.ProcessName),
            SimpleSession.Current, _registry);

        #endregion

        /// <summary>
        /// Entry Point
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ConsoleWindowHelper.FindConsoleWindow();

            _viewModelToShare.ExportViewModel("EntryPoint", _registry);
            _viewModelToShare.Simple = new SimpleViewModel();
            _viewModelToShare.Traditional = new TraditionalViewModel();

            Console.WriteLine("Starting to export ViewModel from Console application...");
            _viewModelToShare.Simple.Message = "Hello world from process " +
                                         new ProcessIdentificationProvider(SimpleSession.Current, SimpleSession.ProcessName).CreateNewIdentity();
            _viewModelToShare.Simple.NestedSimple = new MinimalRemotableViewModel();
            _viewModelToShare.Simple.NestedTraditional = new MinimalViewModel();
            _viewModelToShare.Simple.NestedSimple.Test = "RVM";
            _viewModelToShare.Simple.NestedTraditional.Test = "T";

            _viewModelToShare.Traditional.NestedSimple = new MinimalRemotableViewModel();
            _viewModelToShare.Traditional.NestedTraditional = new MinimalViewModel();
            _viewModelToShare.Traditional.NestedSimple.Test = "RVM";
            _viewModelToShare.Traditional.NestedTraditional.Test = "T";

            ((NetPipeViewModelExporter)_exporter).ImporterCountChanged += new Action<int>(OnImporterCountChanged);
            _exporter.Start();
            Console.WriteLine("Press any key to exit...");

            Console.Read();
        }

        /// <summary>
        /// Event Handler, raised whenever the number of importers is changing-
        /// When no importer is left, show console window.
        /// </summary>
        /// <param name="count"></param>
        private static void OnImporterCountChanged(int count)
        {
            if(count <= 0)
                ConsoleWindowHelper.ShowWindow(true);
            else
                ConsoleWindowHelper.ShowWindow(false);
        }

    }
}
